package com.yuanjin.activity;

import java.security.PublicKey;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;

import com.yuanjin.R;

public class WelcomeActivity extends Activity {
	// 是否首次安装使用软件标志
	private Boolean firstIn = false;
	private static final int GO_HOME = 1000;
	private static final int GO_GUIDE = 1001;
	private int TIME = 2000;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		init();
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case GO_HOME:
				Intent intent = new Intent(WelcomeActivity.this,
						LoginPage.class);
				startActivity(intent);
				finish();
				break;

			case GO_GUIDE:
				Intent intent2 = new Intent(WelcomeActivity.this, Guide.class);
				startActivity(intent2);
				finish();
				break;
			}
		};
	};

	private void init() {
		// 实例化sharePreference对象
		SharedPreferences spPreferences = getSharedPreferences(
				"mySharedPreferences", Activity.MODE_PRIVATE);
		// 从spspPreferences查找数据firstIn，若不存在则新建且返回默认值true
		firstIn = spPreferences.getBoolean("firstIn", true);

		if (!firstIn) {
			// 首次安装使用软件
			mHandler.sendEmptyMessageDelayed(GO_HOME, TIME);
		} else {
			// 非首次，返回GO_HOME 则需要更新firstIn的数据
			mHandler.sendEmptyMessageDelayed(GO_GUIDE, TIME);
			// 实例化SharedPreference.editor对象
			SharedPreferences.Editor editor = spPreferences.edit();
			editor.putBoolean("firstIn", false);
			// 执行更改
			editor.commit();

		}

	}

}
